#include <bits/stdc++.h>

#define N 100050

using namespace std;

int n, m, T;
int a[N], c[N];
int g[20][N];

int Kn = 0, K = 0;

inline void init(){
	int nn = n;
	while(nn - (nn & -nn) > 0) nn -= (nn & -nn);
	nn = n;
	while(nn){K++, nn = nn >> 1;}
	Kn = nn;
}

int main(){
	freopen("arena.in", "r", stdin);
	freopen("arena.out", "w", stdout);
	
	cin >> n >> m;
	init();
	
	for(int i = 1; i <= n; i++) cin >> a[i];
	for(int i = 1; i <= m; i++) cin >> c[i];
	for(int i = 1; i <= K; i++){
		for(int j = 1; j <= Kn >> (i - 1); j++) cin >> g[i][j];
	}
	cin >> T;
	while(T--){
		int x[4];
		cin >> x[0] >> x[1] >> x[2] >> x[3];
		vector <int> v;
		int sum = 0;
		v.push_back(0);
		for(int i = 1; i <= n; i++) v.push_back(a[i] ^ x[i % 4]), sum += v[i];
		for(int i = 1; i <= K; i++){
			for(int j = 1; j <= Kn; j++){
				if(g[i][j] == 0){
					
				}
			}
		}
		cout << sum << "\n";
	}
	
	
	return 0;
}
